<?php
namespace app\modules\supplier\call;
use think\Db;
use think\Hook;
// use app\modules\user\call\Grid as UserGridCall;
use app\modules\supplier\call\Auth as UserAuthCall;
class Status extends CallBase
{
    protected static $_error = array();

    protected static function _initCall($force = false)
    {
        parent::_initBase($force);
    }
    final static function isLogin($auto_login = true)
    {
        self::_initCall();
        if (self::$_is_login) {
            self::_updateUserAuthStatus();
            return true;
        } elseif ($auto_login) {
            return UserAuthCall::isLogin($auto_login);
        }
        return false;
    }

    /**
     * 更新和清理在线用户Auth信息
     * Todo 上云后基于MEM缓存再做具体功能
     */
    final protected static function _updateUserAuthStatus()
    {
        $timer_fast=session('timer_fast');
        $timer_short=session('timer_short');
        $timer_long=session('timer_long');
        if(is_null($timer_fast) || $timer_fast<TIMESTAMP){
            if(self::$_is_login){
                $auth_info=Db::name('supplier_auth')->where(['supplier_user_id'=>self::$_account['supplier_user_id'],'auth_token'=>UserAuthCall::_clientAuthTokenEncode(self::$_account['auth_token'])])->find();
                if(!is_array($auth_info) || empty($auth_info)){
                    //认证信息异常货已过期，强制注销用户登录
                    Db::name('supplier_auth')->where(['auth_id'=>$auth_info['auth_id']])->delete();
                    self::$_error[]="用户会话已过期，请重新登录系统";
                    // $log = ['user_id' => $auth_info['user_id'], 'action_info' => '[' . self::$_account['user_name'] . "] 用户会话已过期，请重新登录系统!"];
                    // Hook::listen('user_log_action', $log);
                    session('account', null);
                    session('account_sign', null);
                    session('account_exptime', null);
                    cookie('client_account', null);
                }else{
                    //同步认证信息
	                if($auth_info['expire_time']-TIMESTAMP<self::$_auth_expire_interval){
	                	//实时会话用户
		                Db::name('supplier_auth')->where(['auth_id'=>$auth_info['auth_id']])->update(['activity_time'=>TIMESTAMP,'expire_time'=>TIMESTAMP+self::$_auth_expire_interval,'has_online'=>1]);
	                }else{
	                	//保持会话用户（保持原始auth_token过期时间不变）
		                Db::name('supplier_auth')->where(['auth_id'=>$auth_info['auth_id']])->update(['activity_time'=>TIMESTAMP,'has_online'=>1]);
	                }
	                //同步自己的会话状态
	                UserAuthCall::updateSelfSessionCookie();
                }
            }
            session('timer_fast',30);
        }else if(is_null($timer_short) || $timer_short<TIMESTAMP){
            Db::name('supplier_auth')->where(['expire_time'=>['<',TIMESTAMP]])->update(['has_online'=>0]);
            session('timer_short',ceil(TIMESTAMP+self::$_auth_expire_interval/3));
        }else if(is_null($timer_long) || $timer_long<TIMESTAMP){
            //用户回话认证数据记录清理，最长保持3个月
            Db::name('supplier_auth')->where(['expire_time'=>['<',TIMESTAMP-60*60*24*90]])->delete();
            session('timer_long',ceil(TIMESTAMP+self::$_auth_expire_interval));
        }
    }
}